{extend name="public/base"/}
{block name="style"}
<link rel="stylesheet" href="__PUBLIC__/css/print.min.css">
{/block}
{block name="content"}
    <div class="data-list" data-url="{:url('index')}">
        <form class="layui-form inline-form">
            <div class="pull-left">
                <div class="layui-inline">
                    <select name="account_id" lay-search lay-filter="data-list">
                        <option value="">销售账号</option>
                        {foreach $accounts as $account}
                        <option value="{$account.account_id}">{$account.username}</option>
                        {/foreach}
                    </select>
                </div>
                <div class="layui-inline">
                    <select name="status" lay-search lay-filter="data-list">
                        <option value="">是否可用</option>
                        {foreach $status as $k=>$v}
                        <option value="{$k}">{$v}</option>
                        {/foreach}
                    </select>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-sm layui-btn-normal search"><i class="layui-icon layui-icon-search"></i></button>
                </div>
                <input type="hidden" id="upload-file">
            </div>
            <div class="pull-right">
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-normal layui-btn-sm ajax-form" title="添加亚马逊箱唛" data-url="{:url('add')}"><i class="layui-icon layui-icon-add-circle"></i> 新增</button>
                </div>
            </div>
        </form>
        <div class="data">
            <p><i class="fa fa-spinner fa-spin"></i> 加载中...</p>
        </div>
    </div>
{/block}
{block name="script"}
<script type="text/javascript" src="__PUBLIC__/js/print.min.js"></script>
<script type="text/javascript">
    layui.use(['layer', 'upload'], function()
    {
        var upload = layui.upload
            , layer = layui.layer;

        upload.render({
            elem: '#upload-file' //绑定元素
            ,url: '{:url("file/upload",["type"=>"document"])}' //上传接口
            , accept: 'file' //普通文件
            , exts: 'pdf' // 指定类型
            , before: function (obj) {
                layer.load(0, {shade: false});
            }
            ,done: function(response)
            {
                layer.closeAll('loading');
                if (response.code == 1)
                {
                    $("#boxcode-path").val(response.data.src);
                    $("#boxcode-name").val(response.data.name);
                }
            }
        });
        // 上传文件
        $("body").on("click", "#upload-btn", function()
        {
            $("#upload-file").click();
        });
        // 打印全部箱唛
        $("body").on("click", ".print-box", function()
        {
           printBox($(this).attr("item"));
        });
        // 打印指定箱唛
        $("body").on("click", ".print-appoint-box", function()
        {
            var boxId = $(this).attr('item');
            layer.prompt({
                btn: ['打印', '取消']
                , title: '指定箱号'
                , formType: 3
            }, function(text, index)
            {
                text *= 1;
                if(isNaN(text))
                {
                    layer.msg("请输入箱号", {icon: 5, time: 1500});
                    return false;
                }
                layer.close(index);
                printBox(boxId, text);
            });
        });

        /**
         * 打印箱唛
         * @param id 打印文档 id
         * @param index 指定箱号，默认全部
         */
        function printBox(id, index)
        {
            index = index || 0;
            layer.load(0, {shade: false});
            $.get("{:url('print')}", {box_id: id, index: index}).done(function(response)
            {
                layer.closeAll('loading');
                if(response.code != 1)
                {
                    layer.msg(response.msg, {icon: 5, time: 1500});
                    return false;
                }
                printJS({printable: response.data.file, type: 'pdf', base64: true, showModal:true})
            });
        }
    });
</script>
{/block}